
// #include "ui.hpp"
#include "screen.hpp"


// ioxx::Pin button_up_pin = BUTTON_UP_PIN_NUM;
// ioxx::Pin button_down_pin = BUTTON_DOWN_PIN_NUM;
// ioxx::Pin button_left_pin = BUTTON_LEFT_PIN_NUM;
// ioxx::Pin button_right_pin = BUTTON_RIGHT_PIN_NUM;
// ioxx::Pin button_ok_pin = BUTTON_OK_PIN_NUM;

ioxx::ButtonTrigger<> button_up;
ioxx::ButtonTrigger<> button_down;
ioxx::ButtonTrigger<> button_left;
ioxx::ButtonTrigger<> button_right;
ioxx::ButtonTrigger<> button_ok;


MenuItem menu_items[MENU_ITEM_COUNT];


Blinker bkr;

CountScreen cs;
AddressScreen as;

beef::InterfaceBase *screen_list[] = {&as, &cs};

uint8_t current_screen_index = 0;


void enter_and_switch_screen() {
    auto *current = screen_list[current_screen_index];
    auto r = current->enter();

    if (r == NAVIGATION_RIGHT) {
        // 切换到其他界面
        current_screen_index++;
        if (current_screen_index >= sizeof(screen_list) / sizeof(screen_list[0])) {
            current_screen_index = 0;  // 循环回到第一个界面
        }
    }
    else if (r == NAVIGATION_LEFT) {
        // 切换到其他界面
        if (current_screen_index > 0) {
            current_screen_index--;
        }
        else {
            current_screen_index = sizeof(screen_list) / sizeof(screen_list[0]) - 1;  // 循环回到最后一个界面
        }
    }
}


TimeType task_scan_button() {
    button_up.feed(digitalRead(BUTTON_UP_PIN_NUM));
    button_down.feed(digitalRead(BUTTON_DOWN_PIN_NUM));
    button_left.feed(digitalRead(BUTTON_LEFT_PIN_NUM));
    button_right.feed(digitalRead(BUTTON_RIGHT_PIN_NUM));
    button_ok.feed(digitalRead(BUTTON_OK_PIN_NUM));

    return 20;
}


void reset_buttons() {
    button_up.reset();
    button_down.reset();
    button_left.reset();
    button_right.reset();
    button_ok.reset();
}


void reset_button_event() {
    button_up.reset_event();
    button_down.reset_event();
    button_left.reset_event();
    button_right.reset_event();
    button_ok.reset_event();
}


void init_buttons() {
    pinMode(BUTTON_UP_PIN_NUM, INPUT_PULLUP);
    pinMode(BUTTON_DOWN_PIN_NUM, INPUT_PULLUP);
    pinMode(BUTTON_LEFT_PIN_NUM, INPUT_PULLUP);
    pinMode(BUTTON_RIGHT_PIN_NUM, INPUT_PULLUP);
    pinMode(BUTTON_OK_PIN_NUM, INPUT_PULLUP);
}